package cate.game.role.pay.trademall.currencyshop;

import cate.common.table.d.GDOperation;
import cate.common.table.trademall.currencyshop.ExchangeTokenRow;
import cate.common.table.trademall.currencyshop.PayM1RebateGroupRow;
import cate.common.table.trademall.currencyshop.PayM1RebateRow;
import cate.common.table.trademall.currencyshop.PayM1Row;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.pay.trademall.currencyshop.msg.ExchangeTokenInfoResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "货币商铺")
public class CurrencyShopPO extends RoleDependent {
    @NoteField(value = "代币首次兑换记录")
    public Map<Integer, Boolean> tokenExchangeRecords;

//    @NoteField(value = "返利记录")
//    public Map<Integer, Integer> rebateRecords;

//    @NoteField(value = "返利期数")
//    public int term;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (tokenExchangeRecords == null) {
            tokenExchangeRecords = new HashMap<>();
        }
//        if (rebateRecords == null) {
//            rebateRecords = new HashMap<>();
//        }
    }

    @Override
    public void onEnter(){
//        if (term == 0) {
//            PayM1RebateRow row = role.getGame().table.tradeMall.m1_rebate.getRebateRow(role.getGame().getOpenTime());
//            term = row == null ? 0 : row.id;
//        }
    }

    /**
     * 查询是否允许充值元宝(幻玉)
     *
     * @param configId 配置ID
     * @return 查询结果 无携带数据
     */
    public GameResult<Void> payRechargeM1Allow(int configId) {
        GameResult<Void> r = new GameResult<>();
        PayM1Row configRow = role.getGame().table.tradeMall.pay_m1.get(configId);
        if (configRow == null) {
            return r.fail("找不到配置");
        }
        return r.success();
    }

    /**
     * 成功充值元宝(幻玉)
     * 发放元宝
     *
     * @param configId 对应配置ID
     */
    public EcResult<Void> onRechargeM1(int configId) {
        GameResult<Void> r = new GameResult<>();
        PayM1Row configRow = role.getGame().table.tradeMall.pay_m1.get(configId);
        if (configRow == null) {
            return r.fail("找不到配置");
        }
        MixRes rewardRes = new MixRes(configRow.m1Str);
        if (!tokenExchangeRecords.containsKey(configId) || !tokenExchangeRecords.get(configId)) {
            rewardRes.addList(new MixRes(configRow.extraM1));
        }
        EcResult<?> r_reward = rewardRes.addCheck(role);
        if (!r_reward.ok()) {
            return r.fail(r_reward.message);
        }
        if (!tokenExchangeRecords.containsKey(configId) || !tokenExchangeRecords.get(configId)) {
            tokenExchangeRecords.put(configId, true);
        }
        new MixResAdder().setRes(rewardRes)
                .setOperation(GDOperation.TRADE_MALL_PAY_M1)
                .setOperationSub(configId)
                .setDialogReward(true)
                .exe(role);
        return r;
    }

    /**
     * 发送幻晶(代币)兑换信息
     */
    public void sendExchangeTokenInfo() {
        role.sendNow(new ExchangeTokenInfoResp(this));
    }


    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
    }
}
